﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Class_DTO;
using System.Data.SqlClient;

namespace Class_DAO
{
    public class NhanVienDAO
    {
        public DataTable getDanhSachNV()
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("getNhanVien", cn);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;

        }

        public DataTable getDanhSachNV_theoten(string pTEN)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("[getNhanVien_theoten]", cn);

            SqlCommand cmd = new SqlCommand("getNhanVien_theoten", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenNV", SqlDbType.NVarChar);
            cmd.Parameters["@tenNV"].Value = pTEN;
            ad.SelectCommand = cmd;
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;
        }

        public void ThemNhanVien_DAO(NhanVienDTO nvDTO)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlCommand cmd = new SqlCommand("insertNV", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maNV", SqlDbType.Int);
            cmd.Parameters.Add("@tenNV", SqlDbType.NVarChar);
            cmd.Parameters.Add("@soDT", SqlDbType.NVarChar);
            cmd.Parameters.Add("@diaChi", SqlDbType.NVarChar);            
            cmd.Parameters.Add("@tenDN", SqlDbType.NVarChar);            
            cmd.Parameters.Add("@matKhau", SqlDbType.NVarChar);
            cmd.Parameters["@maNV"].Direction = ParameterDirection.Output;


            cmd.Parameters["@tenNV"].Value = nvDTO.Tennv;
            cmd.Parameters["@soDT"].Value = nvDTO.Sdt;
            cmd.Parameters["@diaChi"].Value = nvDTO.Diachi;
            cmd.Parameters["@tenDN"].Value = nvDTO.Tendn ;
            cmd.Parameters["@matKhau"].Value = nvDTO.Matkhau ;

            cmd.ExecuteNonQuery();

            int mkh = Convert.ToInt32(cmd.Parameters["@maNV"].Value);
            nvDTO.Manv = mkh;
            cn.Close();
        }
        // sua lam theo mo hinh khac
        public void sua(int ma, string ten, string sdt, string diachi, string tendn, string matkhau)
        {
            SqlParameter[] sp = new SqlParameter[6];
            sp[0] = new SqlParameter("@maNV", ma);
            sp[1] = new SqlParameter("@tenNV", ten);            
            sp[3] = new SqlParameter("@soDT", sdt);
            sp[2] = new SqlParameter("@diaChi", diachi);
            sp[4] = new SqlParameter("@tenDN", tendn);           
            sp[5] = new SqlParameter("@matKhau", matkhau);
            DataProvider.proc("updateNV", sp);
        }
        public void xoa(int s1)
        {
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@maNV", s1);
            DataProvider.proc("deleteNhanVien", sp);
        }
    }
}
